<template>
  <div class="goods-list">
    <!-- <router-link class="goods-item" v-for="(item, index) in goodslist" :key="index"
     :to="'/home/goodsinfo/' + item.id" tag="div">
      <div class="goods-img">
        <img :src="item.img_url" alt>
        <h3>{{ item.title }}</h3>
      </div>
      <div class="goods-body">
        <div class="goods-price">
          <span class="sale">￥{{ item.sell_price }}</span>
          <span class="before">￥{{ item.market_price }}</span>
          <p class="goods-message">
            <span>热卖中</span>
            <span>剩余 {{ item.stock_quantity }} 件</span>
          </p>
        </div>
      </div>
    </router-link>-->

    <!-- 跳转方式：window.localtion.href 编程式导航 -->

    <div
      class="goods-item"
      v-for="(item, index) in goodslist"
      :key="index"
      @click="goDetail(item.id)"
    >
      <div class="goods-img">
        <img :src="item.img_url" alt>
        <h3>{{ item.title }}</h3>
      </div>
      <div class="goods-body">
        <div class="goods-price">
          <span class="sale">￥{{ item.sell_price }}</span>
          <span class="before">￥{{ item.market_price }}</span>
          <p class="goods-message">
            <span>热卖中</span>
            <span>剩余 {{ item.stock_quantity }} 件</span>
          </p>
        </div>
      </div>
    </div>

    <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
  </div>
</template>

<script>
import Toast from 'mint-ui'
export default {
  data() {
    return {
      pageindex: 1, // 分页的页数
      goodslist: [] // 存放商品列表的数组
    };
  },
  created() {
    this.getGoosList();
  },
  methods: {
    getGoosList() {
      // 获取商品列表
      this.$http
        .get("api/getgoods?pageindex=" + this.pageindex)
        .then(result => {
          if (result.body.status === 0) {
            //   this.goodslist = result.body.message;
            this.goodslist = this.goodslist.concat(result.body.message);
          } else {
            Toast("failure");
          }
        });
    },
    getMore() {
      this.pageindex++;
      this.getGoosList();
    },
    goDetail(id) {
      // 使用js 形式进行路由导航
      // 1：
      // this.$router.push('/home/goodsinfo/' + id);
      // 2：传递对象
      // this.$router.push({ path: "/home/goodsinfo/" + id});
      // 3. 传递命名的路由
      this.$router.push({ name: 'goodsinfo', params: { id }})
    }
  }
};
</script>

<style lang="scss" scoped>
.goods-list {
  // 在不影响后续布局的情况下不用清除浮动
  .goods-item {
    float: left;
    width: 48%;
    margin: 2px;
    border: 1px solid #ccc;
    display: flex;
    flex-flow: column;
    justify-content: space-between;
    padding: 5px 5px 0;
    box-shadow: 5px 5px 6px #bbb;

    .goods-img {
      img {
        width: 100%;
        height: 100%;
      }
      h3 {
        font-size: 16px;
        line-height: 20px;
        min-height: 60px;
      }
    }
    .goods-body {
      background-color: #ddd;
      width: 100%;
      padding: 5px 6px 0;
      margin-bottom: 2px;
      .goods-price {
        line-height: 25px;
        .sale {
          color: #f00;
          font-size: 15px;
        }
        .before {
          color: #666;
          font-size: 12px;
          text-decoration: line-through;
        }
        .goods-message {
          display: flex;
          justify-content: space-between;
          font-size: 13px;
        }
      }
    }
  }
}
</style>
